package com.example.api.controller;

import com.example.api.dto.LoginDTO;
import com.example.api.dto.Result;
import com.example.api.entity.User;
import com.example.api.service.AuthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/auth")
public class AuthController {
    private static final Logger logger = LoggerFactory.getLogger(AuthController.class);

    private final AuthService authService;

    public AuthController(AuthService authService) {
        this.authService = authService;
    }

    @PostMapping("/login")
    public Result<Map<String, Object>> login(@RequestBody LoginDTO loginDTO) {
        try {
            logger.debug("收到登录请求: {}", loginDTO.getUsername());
            Map<String, Object> result = authService.login(loginDTO);
            logger.debug("登录成功，返回数据: {}", result);
            return Result.success(result);
        } catch (Exception e) {
            logger.error("登录失败: {}", e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }
} 